﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace ExFlash.Records
{
    public enum CapStyles : byte
    {
        RoundCap = 0,
        NoCap = 1,
        SquareCap = 2
    }

    public enum JoinStyles : byte
    {
        RoundJoin = 0,
        BevelJoin = 1,
        MiterJoin = 2
    }
    
    public class LineStyle2Record : LineStyleRecord
    {
        public CapStyles StartCapStyle
        {
            get;
            protected set;
        }

        public JoinStyles JoinStyle
        {
            get;
            protected set;
        }

        public Boolean HasFillFlag
        {
            get;
            protected set;
        }

        public Boolean NoHScaleFlag
        {
            get;
            protected set;
        }

        public Boolean NoVScaleFlag
        {
            get;
            protected set;
        }

        public Boolean PixelHuntingFlag
        {
            get;
            protected set;
        }

        public Boolean NoClose
        {
            get;
            protected set;
        }

        public CapStyles EndCapStyle
        {
            get;
            protected set;
        }

        public ushort MiteLimitFactor
        {
            get;
            protected set;
        }

        public FillStyleRecord FillType
        {
            get;
            protected set;
        }
        
        public LineStyle2Record()
            : base(ShapeType.Shape4)
        {

        }

        public override Pen GetPen()
        {
            Pen pen = base.GetPen();

            switch (StartCapStyle)
            {
                case CapStyles.RoundCap:
                    {
                        pen.StartCap = LineCap.Round;
                        break;
                    }
                case CapStyles.NoCap:
                    {
                        pen.StartCap = LineCap.Flat;
                        break;
                    }
                case CapStyles.SquareCap:
                    {
                        pen.StartCap = LineCap.Square;
                        break;
                    }
            }

            switch (EndCapStyle)
            {
                case CapStyles.RoundCap:
                    {
                        pen.EndCap = LineCap.Round;
                        break;
                    }
                case CapStyles.NoCap:
                    {
                        pen.EndCap = LineCap.Flat;
                        break;
                    }
                case CapStyles.SquareCap:
                    {
                        pen.EndCap = LineCap.Square;
                        break;
                    }
            }

            switch (JoinStyle)
            {
                case JoinStyles.RoundJoin:
                    {
                        pen.LineJoin = LineJoin.Round;
                        break;
                    }
                case JoinStyles.BevelJoin:
                    {
                        pen.LineJoin = LineJoin.Bevel;
                        break;
                    }
                case JoinStyles.MiterJoin:
                    {
                        pen.LineJoin = LineJoin.Miter;
                        break;
                    }
            }

            if (JoinStyle == JoinStyles.MiterJoin)
            {
                pen.MiterLimit = MiteLimitFactor;
            }

            return pen;
        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            Width = reader.ReadUInt16();

            StartCapStyle = (CapStyles)reader.ReadUInt32(2);

            JoinStyle = (JoinStyles)reader.ReadUInt32(2);

            HasFillFlag = reader.ReadBoolean();

            NoHScaleFlag = reader.ReadBoolean();

            NoVScaleFlag = reader.ReadBoolean();

            // Reserved
            reader.SkipBits(5);

            NoClose = reader.ReadBoolean();

            EndCapStyle = (CapStyles)reader.ReadUInt32(2);

            reader.AlignBits();

            if (JoinStyle == JoinStyles.MiterJoin)
            {
                MiteLimitFactor = reader.ReadUInt16();
            }

            if (!HasFillFlag)
            {
                Color = new RGBARecord();
                Color.SwfDeserialize(reader);
            }
            else
            {
                FillType = new FillStyleRecord(ShapeType);
                FillType.SwfDeserialize(reader);
            }

        }
    }
}
